$(function () {
    $("#jqGrid").Grid({
        url: '../income/list',
        colModel: [
            {label: 'id', name: 'id', index: 'id', key: true, hidden: true},
            {label: '用户编号', name: 'userid', index: 'userid', width: 20},
            {label: '用户昵称', name: 'userName', index: 'userName', width: 40},
            {label: '用户手机号', name: 'userPhone', index: 'userPhone', width: 30},
            {label: '订单编号', name: 'orderno', index: 'orderno', width: 60},
            {label: '订单金额(元)', name: 'ordermoney', index: 'ordermoney', width: 30},
            {label: '收益(元)', name: 'mymoney', index: 'mymoney', width: 30},
            {label: '消费者编号', name: 'consumUid', index: 'consumUid', width: 30},
            {label: '消费者手机', name: 'consumPhone', index: 'consumPhone', width: 30},
            {label: '比率', name: 'rate', index: 'rate', width: 20},
            {
                label: '状态', name: 'status', index: 'status', width: 30, formatter: function (value) {
                    if (value == 1) {
                        return '有效';
                    } else {
                        return '无效';
                    }
                }
            },
            {
                label: '收益类型', name: 'type', index: 'type', width: 30, formatter: function (value) {
                    if (value == 1) {
                        return 'Done奖励';
                    } else if(value == 2){
                        return '售出产品';
                    }else if(value == 3){
                        return '购买兑换产品';
                    }else if(value == 4){
                        return '用户转出';
                    }else if(value == 5){
                        return '用户转入';
                    }else if(value == 6){
                        return '推荐奖励';
                    }
                }
            },
            {
                label: '创建日期', name: 'createdDate', index: 'createdDate', width: 60, formatter: function (value) {
                    return transDate(value, 'yyyy-MM-dd hh:mm:ss');
                }
            }]
    });
});

let vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        income: {},
        ruleValidate: {
            name: [
                {required: true, message: '名称不能为空', trigger: 'blur'}
            ]
        },
        q: {
            userPhone: '',
            orderno: ''
        }
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.income = {};
        },
        update: function (event) {
            let id = getSelectedRow("#jqGrid");
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id)
        },
        saveOrUpdate: function (event) {
            let url = vm.income.id == null ? "../income/save" : "../income/update";
            Ajax.request({
                url: url,
                params: JSON.stringify(vm.income),
                type: "POST",
                contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
            });
        },
        del: function (event) {
            let ids = getSelectedRows("#jqGrid");
            if (ids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                Ajax.request({
                    url: "../income/delete",
                    params: JSON.stringify(ids),
                    type: "POST",
                    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });
            });
        },
        getInfo: function (id) {
            Ajax.request({
                url: "../income/info/" + id,
                async: true,
                successCallback: function (r) {
                    vm.income = r.income;
                }
            });
        },
        reload: function (event) {
            vm.showList = true;
            let page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {'userPhone': vm.q.userPhone,"orderno":vm.q.orderno},
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
        },
        reloadSearch: function () {
            vm.q = {
                userPhone: '',
                orderno: ''
            }
            vm.reload();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        }
    }
});